package com.atguigu.lease.web.admin.service.impl;

import com.atguigu.lease.model.entity.*;
import com.atguigu.lease.model.enums.ItemType;
import com.atguigu.lease.web.admin.mapper.*;
import com.atguigu.lease.web.admin.service.*;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentDetailVo;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentItemVo;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentQueryVo;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentSubmitVo;
import com.atguigu.lease.web.admin.vo.fee.FeeValueVo;
import com.atguigu.lease.web.admin.vo.graph.GraphVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @author liubo
 * @description 针对表【apartment_info(公寓信息表)】的数据库操作Service实现
 * @createDate 2023-07-24 15:48:00
 */
@Service
public class ApartmentInfoServiceImpl extends ServiceImpl<ApartmentInfoMapper, ApartmentInfo>
        implements ApartmentInfoService {

    @Autowired
    private ApartmentFacilityService apartmentFacilityService;

    @Autowired
    private ApartmentLabelService apartmentLabelService;

    @Autowired
    private ApartmentFeeValueService apartmentFeeValueService;

    @Autowired
    private GraphInfoService graphInfoService;

    @Autowired
    private GraphInfoMapper graphInfoMapper;

    @Autowired
    private LabelInfoMapper labelInfoMapper;

    @Autowired
    private FacilityInfoMapper facilityInfoMapper;

    @Autowired
    private FeeValueMapper feeValueMapper;

    @Override
    public IPage<ApartmentItemVo> getPageList(Page<ApartmentItemVo> apartmentItemVoPage, ApartmentQueryVo queryVo) {
        return baseMapper.getPageList(apartmentItemVoPage, queryVo);
    }

    @Override
    public void saveOrUpdateApartmentInfo(ApartmentSubmitVo apartmentSubmitVo) {
        //mybatis自带的保存和更新的方法
        saveOrUpdate(apartmentSubmitVo);

        //保存公寓配套

        //获取配套的id，前端传过来的配套信息不止一个
        List<Long> facilityInfoIds = apartmentSubmitVo.getFacilityInfoIds();
        //创建一个集合存储信息
        ArrayList<ApartmentFacility> apartmentFacilitiesArrayList = new ArrayList<>();
        //保险起见验空
        if (!CollectionUtils.isEmpty(facilityInfoIds)) {
            for (Long facilityInfoId : facilityInfoIds) {
                //属性及进行赋值
                ApartmentFacility apartmentFacility = ApartmentFacility
                        .builder()
                        .facilityId(facilityInfoId)
                        .apartmentId(apartmentSubmitVo.getId())
                        .build();
                //把遍历出来的属性放到集合里面
                apartmentFacilitiesArrayList.add(apartmentFacility);
            }
            //批量保存需要用到公寓配套表
            //调用批量保存的方法将集合放进去
            apartmentFacilityService.saveBatch(apartmentFacilitiesArrayList);
        }

        //保存公寓标签

        //现货去标签们
        List<Long> labelIds = apartmentSubmitVo.getLabelIds();
        //创建一个集合
        ArrayList<ApartmentLabel> apartmentLabels = new ArrayList<>();
        //非空判断
        if (!CollectionUtils.isEmpty(labelIds)) {
            //遍历标签们
        for (Long labelId : labelIds) {

                ApartmentLabel build = ApartmentLabel.builder()
                        .labelId(labelId)
                        .apartmentId(apartmentSubmitVo.getId())
                        .build();
                //将遍历出来的东西放到集合里面
                apartmentLabels.add(build);
            }
            //调用公寓标签的批量增加的方法，先将公寓标签注入过来
            apartmentLabelService.saveBatch(apartmentLabels);
        }
        //保存公寓杂费


        //先获取标签
        List<Long> feeValueIds = apartmentSubmitVo.getFeeValueIds();
        //创建集合
        ArrayList<ApartmentFeeValue> apartmentFeeValues = new ArrayList<>();
        //判断非空
        if (!CollectionUtils.isEmpty(feeValueIds)){
            //遍历集合
            for (Long feeValueId : feeValueIds) {
                //取出来元素
                ApartmentFeeValue build = ApartmentFeeValue.builder()
                        .feeValueId(feeValueId)
                        .apartmentId(apartmentSubmitVo.getId())
                        .build();
                //再放到集合里
                apartmentFeeValues.add(build);
            }
            //统一插入
            apartmentFeeValueService.saveBatch(apartmentFeeValues);
        }

            //先获取公寓图片
        List<GraphVo> graphVoList = apartmentSubmitVo.getGraphVoList();
        if (!CollectionUtils.isEmpty(graphVoList)){
            //创建集合
            ArrayList<GraphInfo> graphInfos = new ArrayList<>();
            //遍历集合
            for (GraphVo graphVo : graphVoList) {
                //先创建一个Graphvo对象
                GraphInfo graphInfo = new GraphInfo();
                //设置图片的名字
                graphInfo.setName(graphVo.getName());
                //地址
                graphInfo.setUrl(graphVo.getUrl());
                //公寓id
                graphInfo.setItemId(apartmentSubmitVo.getId());
                //图片类型
                graphInfo.setItemType(ItemType.APARTMENT);
                //添加集合
                graphInfos.add(graphInfo);
            }
            //批量加进去
            graphInfoService.saveBatch(graphInfos);
        }

    }

    /**
     * 根据id查询详细信息
     * @param id
     * @return
     */
    @Override
    public ApartmentDetailVo getApartmentInfoById(Long id) {
        //1,创建一个返回对象,目前这个对象是空的，
        ApartmentDetailVo apartmentDetailVo = new ApartmentDetailVo();

        //自带的查询方法
        ApartmentInfo apartmentInfo = getById(id);
        //根据公寓id获取图片
        List<GraphVo> graphVoList = graphInfoMapper.genjuidchatupian(id, ItemType.APARTMENT);
        //查标签
        List<LabelInfo> labelInfoList = labelInfoMapper.getjuidchabiaoqian(id);
        //查配套
        List<FacilityInfo> facilityInfoList = facilityInfoMapper.getjuidchapeitao(id);
        //查杂费
        List<FeeValueVo> feeValueVoList = feeValueMapper.getjuidchazafei(id);

        //将查出来的结果全都放到对象里面
        BeanUtils.copyProperties(apartmentInfo,apartmentDetailVo);
        apartmentDetailVo.setGraphVoList(graphVoList);
        apartmentDetailVo.setLabelInfoList(labelInfoList);
        apartmentDetailVo.setFacilityInfoList(facilityInfoList);
        apartmentDetailVo.setFeeValueVoList(feeValueVoList);

        return apartmentDetailVo;
    }


}




